import pinyin from 'js-pinyin'
import _ from 'lodash'

export const StringUtil = {
  toPinyinFull: function(str) {
    if (str === undefined || str === null) {
      return ''
    }
    return pinyin.getFullChars(str)
  },
  toPinyinCamel: function(str) {
    if (str === undefined || str === null) {
      return ''
    }
    return pinyin.getCamelChars(str)
  },
  spaceToUnderline(str) {
    console.log(_.snakeCase(str))
    return _.toUpper(_.snakeCase(str))
  },
  parseHtml(html) {
    const retHtml = html.replace(/<img/gi, '<img style="max-width:100%;height:auto;float:left;display:block;" ')
      .replace(/<section/g, '<div')
      .replace(/\/section>/g, '\div>')
    return retHtml
  },
  // 生成随机串（字符+数字）
  randomString: function(len) {
    const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' /** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1*** */
    const maxPos = $chars.length
    let pwd = ''
    for (let i = 0; i < len; i += 1) {
      pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
    }
    return pwd
  },
  // 生成随机串
  randomNumString: function(len) {
    const $chars = '1234567890' /** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1*** */
    const maxPos = $chars.length
    let pwd = ''
    for (let i = 0; i < len; i += 1) {
      pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
    }
    return pwd
  },
}
